package com.yaozheng.vocationaltraining.adapter;

import android.app.Activity;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.yaozheng.vocationaltraining.R;
import com.yaozheng.vocationaltraining.bean.CategoryItemBean;

import java.util.List;

/**
 * Created by moon on 2017/11/7.
 */

public class PoWoDictionaryItemAdapter extends BaseAdapter {
    private LayoutInflater mInflater;//
    private Activity mContext;

    private List<CategoryItemBean> data = null;
    private int categoriyId ;

    private int selectedPosition = -1;

    public PoWoDictionaryItemAdapter(Activity context,
                                         List<CategoryItemBean> data) {
        this.mInflater = LayoutInflater.from(context);
        this.mContext = context;
        this.data = data;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(
                    R.layout.category_item_list_item, null);
            holder = new ViewHolder();

            holder.open_closed_icon_imageview = (ImageView) convertView
                    .findViewById(R.id.open_closed_icon_imageview);
            holder.cate_name_textview = (TextView) convertView
                    .findViewById(R.id.cate_name_textview);

            holder.title_textview = (TextView) convertView
                    .findViewById(R.id.title_textview);
            holder.detail_textview = (TextView) convertView
                    .findViewById(R.id.detail_textview);
            holder.click_num_textview = (TextView) convertView
                    .findViewById(R.id.click_num_textview);
            holder.time_textview = (TextView) convertView
                    .findViewById(R.id.time_textview);

            convertView.setTag(holder);//
        } else {
            holder = (ViewHolder) convertView.getTag();//
        }

        holder.cate_name_textview.setText("【"+data.get(position).getCateName()+"】");
        if(categoriyId > 0){
            holder.cate_name_textview.setVisibility(View.GONE);
        }else{
            holder.cate_name_textview.setVisibility(View.VISIBLE);
        }
        holder.title_textview.setText(data.get(position).getTitle());
        holder.detail_textview.setText(data.get(position).getDetail());
        holder.click_num_textview.setText(data.get(position).getClick()+
                "");
        holder.time_textview.setText(data.get(position).getCreatedTime());

        if(selectedPosition == position){
            holder.open_closed_icon_imageview.setImageResource(R.drawable.list_open);
            if(!TextUtils.isEmpty(holder.detail_textview.getText().toString())){
                holder.detail_textview.setVisibility(View.VISIBLE);
            }else{
                holder.detail_textview.setVisibility(View.GONE);
            }
        }else{
            holder.open_closed_icon_imageview.setImageResource(R.drawable.list_close);
            holder.detail_textview.setVisibility(View.GONE);
        }

        return convertView;
    }

    public void setData(List<CategoryItemBean> data,int categoriyId) {
        this.categoriyId = categoriyId;
        this.selectedPosition = -1;
        this.data = data;
    }

    public void setselectedPosition(int selectedPosition) {
        if(this.selectedPosition == selectedPosition){
            this.selectedPosition = -1;
        }else{
            this.selectedPosition = selectedPosition;
        }
        notifyDataSetChanged();;
    }

    class ViewHolder {
        ImageView open_closed_icon_imageview;
        TextView cate_name_textview;
        TextView title_textview;
        TextView detail_textview;
        TextView click_num_textview;
        TextView time_textview;
    }
}
